import com.cinsea.common.Env;
import com.cinsea.common.IDGenerator;
import com.cinsea.common.JdbcDao;
import com.cinsea.hrm.model.Org;
import com.cinsea.hrm.service.HumanService;
import com.cinsea.hrm.service.OrgService;
import com.cinsea.timer.BaseJob;
import com.cinsea.util.NumberUtil;
import com.cinsea.util.StringUtil;
import org.quartz.JobExecutionContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import java.lang.Object;
import java.lang.String;
import java.util.List;
import java.util.Map;

public class BgnSync extends BaseJob {
    public void execute(JobExecutionContext jobExecutionContext) {
        //init 3rd party database
        DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUrl("jdbc:mysql://localhost:9171/SisenMESS-User-DataBase?useUnicode=true&characterEncoding=utf-8");
        ds.setUsername("root");
        ds.setPassword("123");
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(ds);
        //end
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        HumanService humanService = Env.getBean("humanService");
        OrgService orgService = Env.getBean("orgService");
        //org
        String workgroupsql="select workgroupid from workgroup where workgroupid=?";
                List<Org> orgs=orgService.getAllOrgList("1");
                for(Org org:orgs){
                    String orgid=org.getId();
                    List workgroup=jdbcTemplate.queryForList(workgroupsql,new Object[]{orgid});
                    if(workgroup.size()>0) continue;
                    String orgname=org.getObjname();
                    Integer orgorder= NumberUtil.getIntegerValue(org.getObjorder(),0);
                    Org porg=orgService.getParentOrg(org.getId());
                    String pid="";
                    if(porg==null||porg.getId()==null) pid="1";
                            else pid=porg.getId();
                    String insertsql="insert into workgroup(workgroupid,pworkgroupid,workgroupname,sortnum,workgroupisshow) values(?,?,?,?,?)";
                    jdbcTemplate.update(insertsql,new Object[]{orgid,pid,orgname,orgorder,1});
                }
        //org end
        String usersql = "select userid from userlist where userid=?";
        String csusersql="select objid,username,orgid,passwd from cs_user where CLOSED=0 and DELETED=0";
        String groupuserlist="select wid from workgroupuserlist where workgroupid=? and userno=?";
        List<Map> csusermap = jdbcDao.queryForList(csusersql);
        for (Map o : csusermap) {
            try {
                String objid = StringUtil.null2String(o.get("objid"));
                String username = StringUtil.null2String(o.get("username"));
                String orgid = StringUtil.null2String(o.get("orgid"));
                List user=jdbcTemplate.queryForList(usersql,new Object[]{objid});
                String passwd = StringUtil.null2String(o.get("passwd"));
                String objname=StringUtil.null2String(humanService.get(objid).getObjname());
                if(user.size()==0){
                    String insertsql="insert into userlist(userid,userno,userpass,usertname,usersex,userrole,userlevel,userstate) values(?,?,?,?,?,?,?,?)";
                    jdbcTemplate.update(insertsql,new Object[]{objid,username,passwd,objname,0,"Users",5,1});
                }
                List groupuser=jdbcTemplate.queryForList(groupuserlist,new Object[]{orgid,username});
                if(groupuser.size()==0){
                    String insertsql="insert into workgroupuserlist(wid,workgroupid,userno) values(?,?,?)";
                    jdbcTemplate.update(insertsql,new Object[]{IDGenerator.getUnquieID(),orgid,username});
                }
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

        }


    }
}
